package com.example.a12769.myapplication.base.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import android.widget.TextView

import com.bumptech.glide.Glide

class RVViewHolder(private val mcontext: Context, itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun <T : View> getView(viewId: Int): T {
        val view = itemView.findViewById<View>(viewId)
        return view as T
    }

    fun setImageRes(viewId: Int, imgRes: Int): RVViewHolder {
        val view = getView<ImageView>(viewId)
        view.setImageResource(imgRes)
        return this
    }

    fun setImageUrl(viewId: Int, imageUrl: String): RVViewHolder {
        val imageView = getView<ImageView>(viewId)
        Glide.with(mcontext).load(imageUrl).into(imageView)
        //这里是图片加载框架哦
        return this

    }

    fun setText(viewId: Int, text: String): RVViewHolder {
        val textView = getView<TextView>(viewId)
        textView.text = text
        return this
    }

    fun setTextSize(viewId: Int, size: Float): RVViewHolder {
        val textView = getView<TextView>(viewId)
        textView.textSize = size
        return this
    }

    fun setTextColor(viewId: Int, textColorRes: Int): RVViewHolder {
        val textView = getView<TextView>(viewId)
        textView.setTextColor(mcontext.resources.getColor(textColorRes))
        return this
    }

    fun setVisiblePlaceHolder(viewId: Int, visible: Boolean): RVViewHolder {
        val view = getView<View>(viewId)
        view.visibility = if (visible) View.VISIBLE else View.INVISIBLE
        return this
    }

    /**
     * 功能描述：设置各个子控件的点击事件
     * 作者： hg_liuzl@qq.com
     * 时间：2016/12/21 13:28
     * 参数：
     */
    fun setOnClickListener(viewId: Int, o: Any, listener: View.OnClickListener): RVViewHolder {
        val view = getView<View>(viewId)
        view.tag = o
        view.setOnClickListener(listener)
        return this
    }

}
